package com.markerhub.controller;


import com.markerhub.common.lang.Result;
import com.markerhub.entity.TeachingCalling;
import com.markerhub.entity.TeachingCallingDetail;
import com.markerhub.mapper.TeachingCallingDetailMapper;
import com.markerhub.mapper.TeachingCallingMapper;
import com.markerhub.service.TeachingCallingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/teaching-calling")
public class TeachingCallingController {
    @Autowired
    private TeachingCallingService callingService;
    @Autowired
    TeachingCallingMapper callingMapper;
    @Autowired
    TeachingCallingDetailMapper callingDetailMapper;

    @PostMapping("/teacher/start")
    public Result startCalling(@RequestBody Map<String, Integer> params) {
        Integer classId = params.get("classId");
        Integer teacherId = params.get("teacherId");

        if (classId == null || teacherId == null) {
            return Result.fail("参数错误");
        }

        return callingService.startCalling(classId, teacherId);
    }

    @GetMapping("/student/pending")
    public Result getPendingCalls(@RequestParam Integer studentId) {
        return callingService.getStudentPendingCalls(studentId);
    }

    // 学生签到接口
    @PostMapping("/student/answer")
    public Result studentAnswer(@RequestBody Map<String, Integer> params) {
        Integer callId = params.get("callId");
        Integer studentId = params.get("studentId");  // 改为接收 studentId

        if (callId == null || studentId == null) {
            return Result.fail("参数错误");
        }

        return callingService.studentAnswer(callId, studentId);
    }

    @GetMapping("/active")
    public Result getActiveCall(@RequestParam Integer classId) {
        TeachingCalling activeCalling = callingMapper.getActiveCall(classId);
        if (activeCalling != null) {
            // 同时获取点名详情
            List<TeachingCallingDetail> details = callingDetailMapper.getCallingDetails(activeCalling.getId());
            Map<String, Object> data = new HashMap<>();
            data.put("calling", activeCalling);
            data.put("details", details);
            return Result.succ(data);
        }
        return Result.succ(null);
    }

    // 添加获取点名详情的接口
    @GetMapping("/details")
    public Result getCallingDetails(@RequestParam Integer callId) {
        if (callId == null) {
            return Result.fail("参数错误");
        }
        return callingService.getCallingDetails(callId);
    }


}
